
//
//  QJTableView.m
//  QJWork
//
//  Created by qijia on 15/6/1.
//  Copyright (c) 2015年 高中雷. All rights reserved.
//

#import "QJTableView.h"
#import "QJCellConfig.h"
#import "QJBaseTableViewCell.h"
#import "UITableView+FDTemplateLayoutCell.h"
@interface QJTableView()
{
    
}
@property (nonatomic,strong)MJRefreshHeaderView *headerView;
@property(nonatomic,strong) MJRefreshFooterView *footerView;
@end
@implementation QJTableView{
    
}




-(id)initWithTableView:(UITableView *)tableview{
    if (self = [super init]) {
        self.tableview =tableview;
        self.tableview.delegate = self;
        self.tableview.dataSource = self;
        self.dataArray = [[NSMutableArray alloc]init];
        self.b_isDown = NO;
        self.b_isUp = NO;
        self.b_swipeable = NO;
    }
        return self;
    
}


-(void)goDown:(BOOL)isDown withUp:(BOOL)isUp{
    self.b_isDown = isDown;
    self.b_isUp = isUp;
    if(isDown){
        [self goDown:NO];
    }
    if(isUp){
        [self goUp:NO];
    }
}

-(void)goDown:(BOOL)isDown withUp:(BOOL)isUp beginRefreshing:(BOOL)beginRefreshing{
    self.b_isDown = isDown;
    self.b_isUp = isUp;
    if(isDown){
        [self goDown:beginRefreshing];
    }
    if(isUp){
        [self goUp:beginRefreshing];
    }
}

//下拉
-(void)goDown:(BOOL)beginRefreshing{

//    QJRefreshHeader *header = [QJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
//    // 隐藏时间
//    header.lastUpdatedTimeLabel.hidden = YES;
//    // 隐藏状态
//    header.stateLabel.hidden = YES;
//    if(beginRefreshing){
//        [header beginRefreshing];
//    }
    
//    self.tableview.header = header;
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.tableview;
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [self headerRereshing];
    };
    if(beginRefreshing){
        [header beginRefreshing];
    }
    self.headerView = header;

}

//上啦
-(void)goUp:(BOOL)beginRefreshing{
//    MJRefreshBackNormalFooter * foorers = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
//    self.tableview.footer= foorers;
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self.tableview;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [self footerRereshing];
    };
    self.footerView = footer;
}

-(void)headerRereshing{
    if (self.delegate && [self.delegate respondsToSelector:@selector(onGoDownWithControl:)]){
         [self.delegate onGoDownWithControl:self];
    }
}

-(void)footerRereshing{
      if (self.delegate && [self.delegate respondsToSelector:@selector(onGoUpWithControl:)]) {
          [self.delegate onGoUpWithControl:self];
      }
}

#pragma mark - UITableViewDataSource

//返回section有几个
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if(self.delegate && [self.delegate respondsToSelector:@selector(onNumberOfSectionsInTableView:)]){
        return [self.delegate onNumberOfSectionsInTableView:tableView];
    }
    if(self.dataArray.count){
        if([self.dataArray[0] isKindOfClass:[NSArray class]]){
            return [self.dataArray count];
        }else{
            return [self.dataArray count];
        }
    }
    return 0;
}

//数组的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    
    if(self.delegate && [self.delegate respondsToSelector:@selector(onToTableView:numberOfRowsInSection:)]){
        return [self.delegate onToTableView:tableView numberOfRowsInSection:section];
    }
    
    if([self.dataArray[section] isKindOfClass:[NSArray class]]){
         return [(NSArray*)self.dataArray[section] count];
    }else{
        return [self.dataArray count];
    }

}

//显示重用cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QJCellConfig *cellConfig = nil;

    cellConfig = [self cllConfigIndexPath:indexPath];

    QJBaseTableViewCell *cell = (QJBaseTableViewCell *)[cellConfig cellOfCellConfigWithTableView:tableView IndexPath:indexPath withContainer:self.containerDelegate];
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(onGoTableView:cellForRowAtIndexPath:)]){
       return [self.delegate onGoTableView:cell cellForRowAtIndexPath:indexPath];
    }
    return (QJBaseTableViewCell *)cell;
}

-(QJCellConfig *)cllConfigIndexPath:(NSIndexPath *)indexPath{
    QJCellConfig *cellConfig = nil;
    if([self.dataArray[indexPath.section] isKindOfClass:[NSArray class]]){
        cellConfig = self.dataArray[indexPath.section][indexPath.row];
    }else{
        cellConfig = self.dataArray[indexPath.row];
    }
    return cellConfig;
}

#pragma mark - UITableViewDelegate

//返回Cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.delegate && [self.delegate respondsToSelector:@selector(onTableView:heightForRowAtIndexPath:)]){
        return [self.delegate onTableView:tableView heightForRowAtIndexPath:indexPath];
    }else{
        QJCellConfig *cellConfig = nil;
        cellConfig = [self cllConfigIndexPath:indexPath];
        CGFloat f;
        if(self.fb_cellHeight){
            f = [cellConfig fd_cellHeightWithTableView:tableView indexPath:indexPath];
        }else{
             f = [cellConfig cellHeightWithTableView:tableView indexPath:indexPath];
        }
        return f;
    }
    return 44;
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(onGoTableView:willDisplayCell:forRowAtIndexPath:)]){
        return [self.delegate onGoTableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
    }
}


//分组返回大小
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(self.delegate && [self.delegate respondsToSelector:@selector(onHeightForHeaderInSection:withObject:withControl:)]){
        return  [ self.delegate onHeightForHeaderInSection:section withObject:self.dataArray[section] withControl:self];
    }
    return 0;
}

//分组尾部返回大小 当等于零饭hi系统原来的值
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(self.delegate && [self.delegate respondsToSelector:@selector(onHeightForFooterInSection:withObject:withControl:)]){
      return [self.delegate onHeightForFooterInSection:section withObject:self.dataArray[section] withControl:self];
    }
    return 0;
}

//自定义返回view
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * view = nil;
     if(self.b_section){
        if(self.delegate && [self.delegate respondsToSelector:@selector(onHeaderInSection:withObject:withControl:)]){
            view = [self.delegate onHeaderInSection:section withObject:self.dataArray[section] withControl:self ];
        }
     }
    return view;
}

//是否有删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.delegate && [self.delegate respondsToSelector:@selector(onGetCellEditingStyle:withObject:withControl:)]){
         QJCellConfig *cellConfig =self.dataArray[indexPath.section][indexPath.row];
        return  [self.delegate onGetCellEditingStyle:indexPath withObject:cellConfig.data withControl:self];
    }
    return UITableViewCellEditingStyleNone;
}

//编辑时候的状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(onGoEditStyle:withIndexPath:withObject:withControl:)]) {
        indexPath = [NSIndexPath indexPathForRow:indexPath.row  inSection:indexPath.section];
         QJCellConfig *cellConfig =self.dataArray[indexPath.section][indexPath.row];
        [self.delegate onGoEditStyle:editingStyle  withIndexPath:indexPath withObject:cellConfig.data withControl:tableView];
    }
}

//cell反选
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

//cell左话button字体改变
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(onGotableView:titleForDeleteConfirmationButtonForRowAtIndexPath:)]){
        return [self.delegate onGotableView:tableView titleForDeleteConfirmationButtonForRowAtIndexPath:indexPath];
    }
    
    return @"删除";
}

//cell选中的
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([self.dataArray[indexPath.section] isKindOfClass:[NSArray class]]){
        if(self.delegate && [self.delegate respondsToSelector:@selector(onGoCellSelect:withObject:withControl:)]){
            
            QJCellConfig *cellConfig =self.dataArray[indexPath.section][indexPath.row];
            [self.delegate onGoCellSelect:indexPath withObject:cellConfig.data withControl:cellConfig.className];
        }
    }else{
        if(self.delegate && [self.delegate respondsToSelector:@selector(onGoCellSelect:withObject:withControl:)]){
             QJCellConfig *cellConfig =self.dataArray[indexPath.row];
            [self.delegate onGoCellSelect:indexPath withObject:cellConfig.data withControl:cellConfig.className];
        }
    }
}

#pragma mark - --scroll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(OnGoScrollViewDidScroll:)]){
        [self.delegate OnGoScrollViewDidScroll:scrollView];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if(self.delegate && [self.delegate respondsToSelector:@selector(OnGoScrollViewDidEndDragging:willDecelerate:)]){
        [self.delegate OnGoScrollViewDidEndDragging:scrollView willDecelerate:decelerate];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    NSLog(@"3333");
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if(self.delegate && [self.delegate respondsToSelector:@selector(OnGoScrollViewDidEndDecelerating:)]){
        [self.delegate OnGoScrollViewDidEndDecelerating:scrollView];
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(onSctionIndexTitlesForTableView:)]){
        return [self.delegate onSctionIndexTitlesForTableView:tableView];
    }
    return nil;
}

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
  
    if(self.delegate && [self.delegate respondsToSelector:@selector(onTableView:sectionForSectionIndexTitle:atIndex:)]){
        return [self.delegate onTableView:tableView sectionForSectionIndexTitle:title atIndex:index];
    }
    return index;
}


#pragma mark - QJPageServerDelegate
-(NSArray*)getDatas{
    return self.dataArray;
}

// 重新填充数据
-(void)reload:(NSArray *)array withRowAnimation:(UITableViewRowAnimation)animation{
//    [self.dataArray removeAllObjects];
//
//    for (int i = 0; i < [array count]; i++)
//    {
//        [self.dataArray insertObject:[array objectAtIndex:i] atIndex:i];
//    }
    self.dataArray = (NSMutableArray *)array;
    [self inLoadok];
}

//插入数据到指定位置插入某一组的某一行
-(void)insertDatas:(NSMutableArray *)array atIndex:(NSRange)index withRowAnimation:(UITableViewRowAnimation)animation {
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    for (int i = 0; i < [array count]; i++)
    {
        if([self.dataArray[index.location] isKindOfClass:[NSArray class]]){
            [self.dataArray[index.location] insertObject:[array objectAtIndex:i] atIndex:index.length];
        }else{
            [self.dataArray insertObject:[array objectAtIndex:i] atIndex:index.length];
        }
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:index.length];
//        NSIndexPath *indexPath1 = [NSIndexPath indexPathWithIndex:index.length];
        [indexPaths addObject: indexPath];
        
    }
    [self.tableview insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
}

//插入数据到指定位置section没有值的时候插入某一组
-(void)insertSectionDatas:(NSMutableArray *)array atIndex:(int)index withRowAnimation:(UITableViewRowAnimation)animation{
     NSMutableArray * arr = [NSMutableArray arrayWithCapacity:array.count];
    for (int i = 0; i < [array count]; i++)
    {
        [arr addObject:[array objectAtIndex:i]];
    }
    //插入array
    [self.dataArray insertObject:arr atIndex:index];
    
    [self.tableview insertSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:animation];
}


//移除指定位置的数据和整个section
-(void)removeAtIndex:(int)index withRowAnimation:(UITableViewRowAnimation)animation {
   
    if(self.dataArray.count >index){
        [self.dataArray removeObjectAtIndex:index];
        if(self.b_section){
            [self.tableview deleteSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:animation];
        }else{
             [self.tableview deleteRowsAtIndexPaths:self.dataArray withRowAnimation:animation];
        }
    }
}

//带section移除某一个行
-(void)removeSection:(NSRange )range withRowAnimation:(UITableViewRowAnimation)animation{
    
     [[self.dataArray objectAtIndex:range.location] removeObjectAtIndex:range.length];

    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    [indexPaths addObject:[NSIndexPath indexPathForRow:range.length inSection:range.location]];
    [self.tableview deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
    
    if([(NSArray*)[self.dataArray objectAtIndex:range.location] count]==0){
         [self.dataArray removeObjectAtIndex:range.location];
          [self.tableview deleteSections:[NSIndexSet indexSetWithIndex:range.location] withRowAnimation:animation];
    }
    
//    [self.tableview deleteSections:[NSIndexSet indexSetWithIndex:range.location] withRowAnimation:animation];
}



//插入到最后
-(void)insertLast:(NSArray *)array withRowAnimation:(UITableViewRowAnimation)animation {
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    for (int i = 0; i < [array count]; i++)
    {
        [self.dataArray insertObject:[array objectAtIndex:i] atIndex:self.dataArray.count];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:self.dataArray.count];
        [indexPaths addObject: indexPath];
    }
    
    if(self.b_section){
          [self.tableview insertSections:[NSIndexSet indexSetWithIndex:self.dataArray.count] withRowAnimation:animation];
    }else{
         [self.tableview insertRowsAtIndexPaths:array withRowAnimation:animation];
    }
}
//插入到最前
-(void)insertFirst:(NSArray *)array withRowAnimation:(UITableViewRowAnimation)animation {
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    for (int i = 0; i < [array count]; i++)
    {
        [self.dataArray insertObject:[array objectAtIndex:i] atIndex:i];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:i];
        [indexPaths addObject: indexPath];
    }
    
    if(self.b_section){
        [self.tableview insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:animation];
    }else{
        
        [self.tableview insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
    }
    
}

//通知控件数据已经加载，但是是空数据
-(void)loaded{
    
}

//清除所有数据
-(void)clear{
    [self.dataArray removeAllObjects];
    [self.tableview reloadData];
}

#pragma mark -  刷新数据
-(void)inLoadok{
   
    [self.tableview reloadData];
    
    if(self.b_isUp){
//        [self.tableview.footer endRefreshing];
        [self.footerView endRefreshing];
    }
    if(self.b_isDown){
//        [self.tableview.header endRefreshing];
        [self.headerView endRefreshing];
    }

    
    
}











@end
